﻿#include "stackplateinfowid.h"
#include <QMessageBox>

StackPlateInfoWid::StackPlateInfoWid(QWidget* parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	ui.spinBox_4->setValue(0);
	ui.spinBox_4->setDisabled(true);
	ui.groupBox->setDisabled(true);
	ui.groupBox_2->setDisabled(true);
}

StackPlateInfoWid::~StackPlateInfoWid()
{}

bool StackPlateInfoWid::Init(StackPlate* plate)
{
	if (plate == nullptr) {
		return false;
	}
	m_plate = plate;
	ui.label_2->setText(m_plate->GetPlateId());
	return true;
}

void StackPlateInfoWid::ShowInfo()
{
	m_state.clear();
	m_state = m_plate->GetPlateStateInfo();
	ui.comboBox->setCurrentIndex(m_state.plate ? 1 : 0);
	ui.spinBox_4->setValue(m_state.num);
	if (m_state.sku.valied) {
		ui.lineEdit->setText(m_state.sku.skuNo);
		ui.spinBox->setValue(m_state.sku.size.length);
		ui.spinBox_2->setValue(m_state.sku.size.width);
		ui.spinBox_3->setValue(m_state.sku.size.height);
		ui.lineEdit_2->setText(QString::number(m_state.stack.type));
		ui.lineEdit_3->setText(m_state.stack.name);
		ui.spinBox_5->setValue(m_state.stack.max);
		ui.spinBox_6->setValue(m_state.stack.ceng);
	} else {
		ui.lineEdit->setText("");
		ui.spinBox->setValue(0);
		ui.spinBox_2->setValue(0);
		ui.spinBox_3->setValue(0);
		ui.lineEdit_2->setText(0);
		ui.lineEdit_3->setText(0);
		ui.spinBox_5->setValue(0);
		ui.spinBox_6->setValue(0);
	}
}

void StackPlateInfoWid::on_pushButton_clicked()
{
	plate::PlateState state = {};
	state.plate = ui.comboBox->currentIndex() == 1 ? true : false;
	if (state.plate) {
		state.num = ui.spinBox_4->value();
		if (state.num > 0) {
			state.sku.skuNo = ui.lineEdit->text();
			state.sku.size.length = ui.spinBox->value();
			state.sku.size.width = ui.spinBox_2->value();
			state.sku.size.height = ui.spinBox_3->value();
			state.stack.type = ui.lineEdit_2->text().toInt();
			state.stack.name = ui.lineEdit_3->text();
			state.stack.max = ui.spinBox_5->value();
			state.stack.ceng = ui.spinBox_6->value();
		}
	}
	bool ret = false;
	if (m_state.plate != state.plate) {
		ret = true;
	}

	if (m_state.num != state.num) {
		ret = true;
	}

	if (ret) {
		QMessageBox::StandardButton but = QMessageBox::question(this, "问题", "检测到托盘数据发生更改，请确认是否修改");
		if (but == QMessageBox::StandardButton::Yes) {
			if (!m_plate->ManualChangePlateState(state)) {
				QMessageBox::warning(this, "ERROR", "修改失败");
			}
		}
	}

}

void StackPlateInfoWid::on_comboBox_currentIndexChanged(int index)
{
	if (index == 0) {
		ui.spinBox_4->setValue(0);
		ui.spinBox_4->setDisabled(true);
		ui.groupBox->setDisabled(true);
		ui.groupBox_2->setDisabled(true);
	} else {
		ui.spinBox_4->setValue(m_state.num);
		ui.spinBox_4->setDisabled(false);
		ui.groupBox->setDisabled(false);
	}
}

